package com.ycsj.goldmedalnewconcept.master.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.github.aakira.expandablelayout.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.parent.adapter.ClassMsgRvAdapter;
import com.ycsj.goldmedalnewconcept.parent.bean.MsgInfo;
import com.ycsj.goldmedalnewconcept.teacher.fragment.LazyBaseFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 班级信息
 * Created by Administrator on 2018/7/17 0017.
 */

public class MasterClassMessageFragment extends LazyBaseFragment {

    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private AVLoadingIndicatorView avi;
    private RelativeLayout rlLoading;

    //数据集合
    private List<MsgInfo> oldDatas;

    //老师账号和角色
    private String c, role;


    //空视图
    private View emptyView;
    private ClassMsgRvAdapter adapter;

    //单例模式
    private static class LazyHolder {
        private static final MasterClassMessageFragment INSTANCE = new MasterClassMessageFragment();
    }

    public static final MasterClassMessageFragment newInstance(String c, String role) {

        MasterClassMessageFragment masterClassMessageFragment = LazyHolder.INSTANCE;

        Bundle bundle = new Bundle();
        bundle.putString("c", c);
        bundle.putString("role", role);
        masterClassMessageFragment.setArguments(bundle);

        return masterClassMessageFragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.master_fragment_class_msg;
    }


    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        //初始化视图
        initView(view);

        //设置下拉刷新
        initSrf();

        //获取老师账号和角色
        initIntent();

        //请求服务器数据
        http();

    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        rv = view.findViewById(R.id.rv);
        srf = view.findViewById(R.id.srf);
        avi = view.findViewById(R.id.avi);
        rlLoading = view.findViewById(R.id.rl_loading);
        oldDatas = new ArrayList<>();
    }


    /**
     * 设置下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, null);
    }

    /**
     * 获取老师账号和角色
     */
    private void initIntent() {
        role = getArguments().getString("role");
        c = getArguments().getString("c");

    }

    /**
     * 初始化列表
     */
    private void initRv() {
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view_white_bg, null);
        adapter = new ClassMsgRvAdapter(getContext(), c, R.layout.master_rv_class_msg, oldDatas);
    }


    /**
     * 请求数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "29");
        map.put("c", c);
        map.put("role", role);
        LogUtils.iTag("test", HttpAddress.MASTERBASEMSG + "?type=29&c=" + c + "&role=" + role);
        HttpUtils.post(HttpAddress.MASTERBASEMSG, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                ToastUtils.showShort(BaseApplication.ERROR);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    Log.i("test", "返回:" + response);

                    initData(response);

                    //初始化列表
                    initRv();

                    //设置适配器
                    BqaManager.setRv(emptyView, getContext(), adapter, rv);

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 根据返回的异型JSON组装页面数据的方法
     */
    private void initData(String json) {
        try {
            JSONObject obj = new JSONObject(json);
            JSONArray array = obj.getJSONArray("classes");

            if (null != array && array.length() != 0) {
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = obj.getJSONObject("class" + (i + 1));
                    MsgInfo msgInfo = new MsgInfo();
                    //复制
                    msgInfo.averageAttendance = object.getDouble("averageAttendance");
                    msgInfo.className = object.getString("name");
                    msgInfo.averageSelef = object.getInt("averageSelef");
                    msgInfo.icon = object.getInt("icon");
                    msgInfo.num = object.getInt("num");
                    msgInfo.payyes = object.getInt("payyes");
                    msgInfo.payno = object.getInt("payno");
                    msgInfo.name = object.getString("name");
                    msgInfo.averageExam = object.getDouble("averageExam");
                    oldDatas.add(msgInfo);
                }
            }

            //添加折叠动画效果
            for (int i = 0; i < oldDatas.size(); i++) {
                oldDatas.get(i).interpolator = Utils.createInterpolator(Utils.LINEAR_OUT_SLOW_IN_INTERPOLATOR);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
